package name.weily.liba.widgets

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import name.weily.liba.tools.logd
import java.util.*


open class SelectionRecyclerViewAdapter<T> constructor(
    override val createView: ((parent: ViewGroup, viewType: Int)-> View),
    override val bindVH: ((holder: ViewHolder, item: T?, position: Int)-> Unit))

    : RecyclerViewAdapter<T>(createView, bindVH) {

    constructor() : this(createView = { parent, viewType ->
        LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
    }, bindVH = { vh, item, pos ->
        vh.getView<TextView>(android.R.id.text1)?.text = item.toString()
    })

    var selectionListener: ((T, Boolean) -> Unit)? = null
    private val selections: MutableMap<Int, T> by lazy { mutableMapOf<Int, T>() }
    var isSelectionMode = false
                    set(value) {
                        field = value
                        if (!field && selections.isNotEmpty()) {
                            selections.clear()
                            notifyDataSetChanged()
                        }
                    }

    private fun addSelection(pos: Int) {
        selections[pos] = mutableList[pos]
        notifyItemChanged(pos)
    }

    private fun removeSelection(pos: Int) {
        selections.remove(pos)
        notifyItemChanged(pos)
    }

    fun isSelected(position: Int) = selections.keys.contains(position)

    fun selections() : Map<Int, T> = selections

    override fun setDataList(list: List<T>) {
        isSelectionMode = false
        super.setDataList(list)
    }

    override fun clearData() {
        isSelectionMode = false
        super.clearData()
    }

    override fun appendDataList(list: List<T>) {
        isSelectionMode = false
        super.appendDataList(list)
    }

    override fun removeAt(position: Int) {
        isSelectionMode = false
        super.removeAt(position)
    }

    override fun insertAt(position: Int, item: T) {
        isSelectionMode = false
        super.insertAt(position, item)
    }

    override fun replaceAt(position: Int, item: T) {
        isSelectionMode = false
        super.replaceAt(position, item)
    }



    override fun onClick(v: View) {
        itemClickListener?.let {
            val tag = v.tag
            if (tag is Int) {
                if (isSelectionMode) {
                    if (isSelected(tag)) {
                        removeSelection(tag)
                        selectionListener?.invoke(getItemData(tag)!!, false)
                    } else {
                        addSelection(tag)
                        selectionListener?.invoke(getItemData(tag)!!, true)
                    }
                } else {
                    it.invoke(v, getItemData(tag), tag)
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.isSelected = selections.keys.contains(position)
        super.onBindViewHolder(holder, position)

    }
}